/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 */

#include <board.h>
#include<rtthread.h>
#include<rtdevice.h>

#if defined(BSP_USING_DAC1) || defined(BSP_USING_DAC2)
#include "drv_config.h"
#include "dac_config.h"

//#define DRV_DEBUG
#define LOG_TAG             "drv.dac"
#include <drv_log.h>

static DAC_HandleTypeDef dac_config[] =
{
#ifdef BSP_USING_DAC1
    DAC1_CONFIG,
#endif

#ifdef BSP_USING_DAC2
    DAC2_CONFIG,
#endif
};

struct stm32_dac
{
    DAC_HandleTypeDef DAC_Handler;
    struct rt_dac_device stm32_dac_device;
};

static struct stm32_dac stm32_dac_obj[sizeof(dac_config) / sizeof(dac_config[0])];

static rt_uint32_t stm32_dac_get_channel(rt_uint32_t channel)
{
    rt_uint32_t stm32_channel = 0;

    switch (channel)
    {
    case  1:
        stm32_channel = DAC_CHANNEL_1;
        break;
    case  2:
        stm32_channel = DAC_CHANNEL_2;
        break;
    }

    return stm32_channel;
}

static rt_err_t stm32_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
{
    DAC_HandleTypeDef *stm32_dac_handler;

    RT_ASSERT(device != RT_NULL);
    stm32_dac_handler = device->parent.user_data;

    if (channel != 1 && channel != 2)
    {
        LOG_E("DAC channel must be between 1 and 2.");
        return -RT_ERROR;
    }
    __HAL_DAC_ENABLE(stm32_dac_handler,stm32_dac_get_channel(channel));

    return RT_EOK;
}

rt_err_t stm32_dac_disabled(struct rt_dac_device *device, rt_uint32_t channel)
{
    DAC_HandleTypeDef *stm32_dac_handler;

    RT_ASSERT(device != RT_NULL);
    stm32_dac_handler = device->parent.user_data;

    if (channel != 1 && channel != 2)
    {
        LOG_E("DAC channel must be between 1 and 2.");
        return -RT_ERROR;
    }
    __HAL_DAC_DISABLE(stm32_dac_handler,stm32_dac_get_channel(channel));
    
    return RT_EOK;
}

static rt_err_t stm32_set_dac_value(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
    DAC_HandleTypeDef *stm32_dac_handler;

    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(value != RT_NULL);

    stm32_dac_handler = device->parent.user_data;

    if (channel == 1 || channel == 2)
    {
        DAC_ChannelConfTypeDef sConfig = {0};
        /** DAC channel OUTconfig
        */
#if defined(SOC_SERIES_STM32H7)
        sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
#endif
        sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
        sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
#if defined(SOC_SERIES_STM32H7)
        sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_DISABLE;
        sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
#endif
        HAL_DAC_ConfigChannel(stm32_dac_handler, &sConfig, stm32_dac_get_channel(channel));

        /* set stm32 DAC channel */
        HAL_DAC_SetValue(stm32_dac_handler, stm32_dac_get_channel(channel), DAC_ALIGN_12B_R,*value);
        HAL_DAC_Start(stm32_dac_handler, stm32_dac_get_channel(channel));
    }
    else
    {
        LOG_E("DAC channel must be between 1 and 2.");
        return -RT_ERROR;
    }

    return RT_EOK;
}

static const struct rt_dac_ops stm_dac_ops =
{
    .enabled = stm32_dac_enabled,
    .disabled = stm32_dac_disabled,
    .convert = stm32_set_dac_value,
};

static int stm32_dac_init(void)
{
    int result = RT_EOK;
    /* save dac name */
    char name_buf[5] = {'d', 'a', 'c', '1', 0};
    int i = 0;
    
    for (i = 0; i < sizeof(dac_config) / sizeof(dac_config[0]); i++)
    {
        /* DAC init */
        stm32_dac_obj[i].DAC_Handler = dac_config[i];
#if defined(DAC1)
        if (stm32_dac_obj[i].DAC_Handler.Instance == DAC1)
        {
            name_buf[3] = '1';
        }
#endif
#if defined(DAC2)
        if (stm32_dac_obj[i].DAC_Handler.Instance == DAC2)
        {
            name_buf[3] = '2';
        }
#endif

        if (HAL_DAC_Init(&stm32_dac_obj[i].DAC_Handler) != HAL_OK)
        {
            LOG_E("%s init failed", name_buf);
            result = -RT_ERROR;
        }
        /* register DAC device */
        if (rt_hw_dac_register(&stm32_dac_obj[i].stm32_dac_device, name_buf, &stm_dac_ops, &stm32_dac_obj[i].DAC_Handler) == RT_EOK)
        {
            LOG_D("%s init success", name_buf);
        }
        else
        {
            LOG_E("%s register failed", name_buf);
            result = -RT_ERROR;
        }
    }

    return result;
}
INIT_BOARD_EXPORT(stm32_dac_init);

#endif /* BSP_USING_DAC */
